/****************************************************
	文件：UIScriptGeneratorEditor.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/3/24 10:45:7
	功能：UI script 生成工具（编辑器）
*****************************************************/

using UnityEngine;
using HTFW.Consts;
using HTFW.Logger;
using UnityEditor;

namespace HTFW.UI
{
    public class UIScriptGeneratorEditor : EditorWindow
    {
        /// <summary>
        /// 窗口实例
        /// </summary>
        static EditorWindow mWnd;

        string mUIName; //ui name

        [MenuItem(MenuPath.UI_SCRIPTS_GENERATOR_PATH, false, 8)]
        private static void MenuClick()
        {
            Init();
            mWnd.Show();
        }

        private void OnGUI()
        {
            Draw();
        }

        private static void Init()
        {
            mWnd = GetWindow<UIScriptGeneratorEditor>("UI脚本生成工具");
        }

        void Draw()
        {
            GUILayout.Label("输入 UI name", new GUIStyle
            {
                fontSize = 16,
                fontStyle = FontStyle.Bold,
            });

            mUIName = EditorGUILayout.TextField("UI name", mUIName);

            //生成
            if (GUILayout.Button("生成"))
                OnClickGenerateBtn();
        }

        void OnClickGenerateBtn()
        {
            GenerateScript(mUIName);
            mWnd.Close();
            AssetDatabase.Refresh();
            HLog.Debug($"{mUIName} scripts generate done.");
        }

        void GenerateScript(string uiName)
        {
            UIScriptGenerator generator = new UIScriptGenerator(uiName);
            generator.Generate(UIGeneratorType.Model);
            generator.Generate(UIGeneratorType.View);
            generator.Generate(UIGeneratorType.Controller);
        }
    }
}